<template>
  <div class="main" direction="column" align="stretch">
    <div class="body">
      <div class="section">
        <div class="section-title">登录页设置</div>
        <flexbox class="login-config">
          <div class="login-config-left">
            <flexbox
              v-if="
                loginForm.companyName ||
                loginForm.companyLoginLogo ||
                loginForm.companyLoginImg
              "
              class="login-config-left-inner"
              :style="{
                background: `url(${baseUrl}${loginForm.companyLoginImg}) no-repeat center center`,
              }"
            >
              <div>
                <img :src="baseUrl + loginForm.companyLoginLogo" alt="" />
                <b>{{ loginForm.companyName }}</b>
              </div>
              <img src="@/assets/img/admin/login_password.png" alt="" />
            </flexbox>
            <img src="@/assets/img/admin/login_bg_config.png" v-else />
          </div>
          <div class="login-config-right" v-loading="img_loading">
            <el-form
              :model="loginForm"
              :rules="loginRules"
              label-width="92px"
              label-position="left"
              ref="form1"
            >
              <el-form-item
                label="标题LOGO"
                prop="companyLoginLogo"
                class="img_wid"
              >
                <flexbox>
                  <UploadImg
                    :fileType="['png']"
                    :fileSize="2"
                    v-model="loginForm.companyLoginLogo"
                    :load="img_loading"
                    @changeLoadings="changeLoadings1"
                    @imgChange="imgChange1"
                  ></UploadImg>
                  <div class="tips">
                    （图片格式要求 png，大小不超过2M，尺寸为52*52px）
                  </div>
                </flexbox>
              </el-form-item>
              <el-form-item label="登录页标题" prop="companyName">
                <el-input
                  placeholder="请输入登录页标题"
                  v-model="loginForm.companyName"
                  clearable
                  style="width: 418px"
                ></el-input>
                <div class="tips">
                  （要求10个文字以内，例如：公司简称CRM系统）
                </div>
              </el-form-item>
              <el-form-item
                label="展示图片"
                prop="companyLoginImg"
                class="img_wid"
              >
                <flexbox>
                  <UploadImg
                    :fileType="['png', 'jpg', 'jpeg']"
                    :fileSize="5"
                    v-model="loginForm.companyLoginImg"
                    :load="img_loading"
                    @changeLoadings="changeLoadings1"
                    @imgChange="imgChange2"
                  ></UploadImg>
                  <span class="tips"
                    >（图片格式要求 png / jpg /
                    jpeg，大小不超过5M，尺寸为1920*1080px）</span
                  >
                </flexbox>
              </el-form-item>
            </el-form>
          </div>
        </flexbox>
      </div>
      <div class="section-part2">
        <div class="section-title">导航LOGO设置</div>
        <flexbox class="login-config">
          <div class="login-config-left">
            <img
              src="@/assets/img/admin/nav_yuan.png"
              v-if="!loginForm.companyLogo"
              class="logo_config"
              alt=""
            />
            <div v-else class="config_nav">
              <img :src="baseUrl + loginForm.companyLogo" alt="" />
            </div>
          </div>
          <div
            class="login-config-right login-config-right2"
            v-loading="img_loading2"
          >
            <el-form
              :model="loginForm"
              :rules="loginRules"
              label-width="92px"
              label-position="left"
              ref="form2"
            >
              <el-form-item label="导航LOGO" prop="companyLogo" class="img_wid">
                <flexbox>
                  <UploadImg
                    :fileType="['png']"
                    :fileSize="2"
                    v-model="loginForm.companyLogo"
                    :load="img_loading2"
                    @changeLoadings="changeLoadings2"
                    @imgChange="imgChange3"
                  ></UploadImg>
                  <div class="tips">
                    （图片格式要求 png，大小不超过2M，尺寸为206*35px）
                  </div>
                </flexbox>
              </el-form-item>
            </el-form>
          </div>
        </flexbox>
      </div>
      <el-divider></el-divider>
      <div style="float: right">
        <el-button @click="cancelStyleConfig">取消</el-button>
        <el-button type="primary" @click="saveStyleConfig">保存</el-button>
      </div>
    </div>
  </div>
</template>

<script>
import { mapGetters } from "vuex";
import UploadImg from "./components/UploadImg";
import { setAdminConfig, adminSystemIndexAPI } from "@/api/admin/config.js";
export default {
  name: "SystemConfig",
  components: {
    UploadImg,
  },
  data() {
    return {
      loginForm: {
        companyName: "",
        companyLoginLogo: "", //登录页logo
        companyLoginImg: "",
        companyLogo: "",
      },
      loginRules: {
        companyName: [
          {
            required: true,
            trigger: ["blur", "change"],
            message: "登录页标题不能为空",
          },
          {
            validator: (rule, value, callback) => {
              if (value.length > 10) {
                callback(new Error("登录页标题不能超过10个文字"));
              } else {
                callback();
              }
            },
            trigger: ["blur", "change"],
          },
        ],
        companyLoginLogo: [
          {
            required: true,
            trigger: ["blur", "change"],
            message: "logo不能为空",
          },
        ],
        companyLoginImg: [
          {
            required: true,
            trigger: ["blur", "change"],
            message: "展示图片不能为空",
          },
        ],
        companyLogo: [
          {
            required: true,
            trigger: ["blur", "change"],
            message: "导航LOGO不能为空",
          },
        ],
      },
      baseUrl: "",
      img_loading: false,
      img_loading2: false,
    };
  },
  computed: {
    ...mapGetters(["manage"]),
  },
  created() {
    this.baseUrl = this.$baseImgUrl;
    /**
     * 获取配置
     */
    this.adminSystem();
  },
  methods: {
    /**
     * 修改loading
     */
    changeLoadings1(type) {
      this.img_loading = type;
    },
    /**
     * 修改loading
     */
    changeLoadings2(type) {
      this.img_loading2 = type;
    },
    /**
     * 实时验证-标题LOGO
     */
    imgChange1() {
      this.$refs.form1.validateField("companyLoginLogo");
    },
    /**
     * 实时验证-展示图片
     */
    imgChange2() {
      this.$refs.form1.validateField("companyLoginImg");
    },
    /**
     * 实时验证-导航LOGO
     */
    imgChange3() {
      this.$refs.form2.validateField("companyLogo");
    },
    /**
     * 获取配置
     */
    adminSystem() {
      adminSystemIndexAPI().then((res) => {
        this.loginForm = res.data;
      });
    },
    /**
     * 保存
     */
    saveStyleConfig() {
      let isValid = false;
      this.$refs.form1.validate((valid1) => {
        this.$refs.form2.validate((valid2) => {
          if (valid1 && valid2) {
            console.log(this.loginForm);
            setAdminConfig(this.loginForm).then((res) => {
              console.log(res);
              if (res.code == 0) {
                this.$message.success(res.msg);
                this.$store.dispatch("GetLogoAndName");
              }
            });
          }
        });
      });
    },
    // 取消
    cancelStyleConfig() {
      this.$refs.form1.resetFields();
      this.$refs.form2.resetFields();
      this.adminSystem();
    },
  },
};
</script>

<style lang="scss" scoped>
.main {
  height: 100%;
  width: 100%;
  font-family: Microsoft YaHei;
  background: #fff;
}

.body {
  flex: 1;
  overflow-y: auto;
  padding: 16px;
  background-color: white;
  border-radius: $xr-border-radius-base;
  position: relative;
}

.section-title {
  font-weight: bold;
  font-size: 14px;
  margin-bottom: 16px;
  background: #f5f6fb;
  color: #333333;
  height: 40px;
  width: 100%;
  line-height: 40px;
  padding-left: 15px;
}
.login-config {
  .tips {
    font-size: 12px;
    font-weight: 400;
    color: #666666;
  }
  &-left {
    width: 568px;
    margin-right: 33px;
    border: 1px solid #c1c7ce;
    border-radius: 4px;
    overflow: hidden;
    &-inner {
      width: 100%;
      height: 320px;
      justify-content: center;
      > div {
        margin-right: 70px;
        display: flex;
        align-items: center;
        margin-top: -150px;
        img {
          width: 45px;
          height: 45px;
          margin-right: 10px;
        }
      }
      > img {
        width: 179px;
      }
    }
    > img {
      width: 100%;
      height: 320px;
      background: #ffffff;
    }
  }
  &-right {
    flex: 1;
  }
  .logo_config {
    height: 162px !important;
  }
  .config_nav {
    height: 162px !important;
    width: 100%;
    background: url("../../../assets/img/admin/nav_change.png") no-repeat center
      center;
    background-size: cover;
    img {
      width: 206px;
      height: 35px;
      margin-left: 24px;
      margin-top: 6px;
    }
  }
}
.section-part2 {
  margin-top: 36px;
}
::v-deep .img_wid .el-form-item__label {
  line-height: 74px;
}
::v-deep .el-form .el-form-item:last-child {
  margin-bottom: 0;
}
.login-config-right2
  /deep/
  .el-upload-list--picture-card
  .el-upload-list__item {
  width: 80px !important;
  height: 80px !important;
  // height: auto !important;
  margin-top: 20px;
}
.login-config-left-inner {
  background-size: cover !important;
}
</style>

